﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TanHuan.Authorize.Model;

namespace TanHuan.Authorize.DataAccess
{
    public class AuthFunctionDA
    {
        /// <summary>
        /// 获取AuthFunction
        /// </summary>
        /// <returns></returns>
        public List<AuthFunction> GetAuthFunction()
        {
            using (AuthorizeDBEntities db = new AuthorizeDBEntities())
            {
                return db.AuthFunction.ToList();
            }
        }

        /// <summary>
        /// 根据[ParentUnid]获取AuthFunction
        /// </summary>
        /// <param name="parentUnid"></param>
        /// <returns></returns>
        public List<AuthFunction> GetAuthFunctionByParentUnid(int parentUnid)
        {
            using (AuthorizeDBEntities db = new AuthorizeDBEntities())
            {
                return db.AuthFunction.Where(authFunction => authFunction.ParentUnid == parentUnid).ToList();
            }
        }

        /// <summary>
        /// 获取一个AuthFunction实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AuthFunction GetAuthFunctionById(int id)
        {
            using (AuthorizeDBEntities db = new AuthorizeDBEntities())
            {
                return db.AuthFunction.SingleOrDefault(authFunction => authFunction.Unid == id);
            }
        }

        /// <summary>
        /// 创建一个AuthFunction实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public void CreateAuthFunction(AuthFunction entity)
        {
            using (AuthorizeDBEntities db = new AuthorizeDBEntities())
            {
                db.AuthFunction.Add(entity);
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除一个AuthFunction实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public void DeleteAuthFunction(int id)
        {
            using (AuthorizeDBEntities db = new AuthorizeDBEntities())
            {
                AuthFunction entity = db.AuthFunction.SingleOrDefault(authFunction => authFunction.Unid == id);
                if (entity != null)
                {
                    db.AuthFunction.Remove(entity);
                }
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 修改一个AuthApp实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public void EditAuthFunction(AuthFunction entity)
        {
            using (AuthorizeDBEntities db = new AuthorizeDBEntities())
            {
                db.Entry(entity).State = System.Data.EntityState.Modified;
                db.SaveChanges();
            }
        }
    }
}
